#!/usr/bin/env python
# encoding: utf-8
from win_service_manage_helper import get_winservice_path

__author__ = '马志'

from xml.dom.minidom import Document

"""

by: 马志 at: 2016/9/18 14:30
@contact: hautmz@163.com
@file: generate_log4netxml.py
"""


def create_log4net_config(db_type, db_name, db_host, db_port, db_username, db_psd):
    """
    
    by: 马志 at: ${DATE} ${TIME}
    """
    doc = Document()  # 创建DOM文档对象
    log4net = doc.createElement("log4net")
    doc.appendChild(log4net)

    root = doc.createElement("root")
    log4net.appendChild(root)

    level = doc.createElement("level")
    level.setAttribute("value", "all")
    root.appendChild(level)
    appender_ref = doc.createElement("appender-ref")
    appender_ref.setAttribute("ref", "AdoNetAppender")
    root.appendChild(appender_ref)
    appender_ref = doc.createElement("appender-ref")
    appender_ref.setAttribute("ref", "AdoNetAppender")
    root.appendChild(appender_ref)
    appender_ref = doc.createElement("appender-ref")
    appender_ref.setAttribute("ref", "AdoNetAppender")
    root.appendChild(appender_ref)
    appender_ref = doc.createElement("appender-ref")
    appender_ref.setAttribute("ref", "AdoNetAppender")
    root.appendChild(appender_ref)
    appender_ref = doc.createElement("appender-ref")
    appender_ref.setAttribute("ref", "AdoNetAppender")
    root.appendChild(appender_ref)
    appender_ref = doc.createElement("appender-ref")
    appender_ref.setAttribute("ref", "AdoNetAppender")
    root.appendChild(appender_ref)
    appender_ref = doc.createElement("appender-ref")
    appender_ref.setAttribute("ref", "AdoNetAppender")
    root.appendChild(appender_ref)

    appender = doc.createElement("appender")
    appender.setAttribute("name", "AdoNetAppender")
    appender.setAttribute("type", "log4net.Appender.AdoNetAppender")
    log4net.appendChild(appender)

    bufferSize = doc.createElement("bufferSize")
    bufferSize.setAttribute("value", "1")
    appender.appendChild(bufferSize)
    appender.appendChild(create_connectionType_element(doc, db_type))
    appender.appendChild(create_conntection_str_element(doc, db_type, db_name, db_host, db_port, db_username, db_psd))
    commandText = doc.createElement("commandText")
    commandTextStr = "INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"
    commandText.setAttribute("value", commandTextStr)
    appender.appendChild(commandText)

    parameter = doc.createElement("parameter")
    parameterName = doc.createElement("parameterName")
    parameterName.setAttribute("value", "@log_date")
    parameter.appendChild(parameterName)
    dbType = doc.createElement("dbType")
    dbType.setAttribute("value", "DateTime")
    parameter.appendChild(dbType)
    layout = doc.createElement("layout")
    layout.setAttribute("type", "log4net.Layout.RawTimeStampLayout")
    parameter.appendChild(layout)
    appender.appendChild(parameter)

    parameter = doc.createElement("parameter")
    parameterName = doc.createElement("parameterName")
    parameterName.setAttribute("value", "@thread")
    parameter.appendChild(parameterName)
    dbType = doc.createElement("dbType")
    dbType.setAttribute("value", "String")
    parameter.appendChild(dbType)
    size = doc.createElement("size")
    size.setAttribute("value", "255")
    parameter.appendChild(size)
    layout = doc.createElement("layout")
    layout.setAttribute("type", "log4net.Layout.PatternLayout")
    conversionPattern = doc.createElement("conversionPattern")
    conversionPattern.setAttribute("value", "%thread")
    layout.appendChild(conversionPattern)
    parameter.appendChild(layout)
    appender.appendChild(parameter)

    parameter = doc.createElement("parameter")
    parameterName = doc.createElement("parameterName")
    parameterName.setAttribute("value", "@log_level")
    parameter.appendChild(parameterName)
    dbType = doc.createElement("dbType")
    dbType.setAttribute("value", "String")
    parameter.appendChild(dbType)
    size = doc.createElement("size")
    size.setAttribute("value", "50")
    parameter.appendChild(size)
    layout = doc.createElement("layout")
    layout.setAttribute("type", "log4net.Layout.PatternLayout")
    conversionPattern = doc.createElement("conversionPattern")
    conversionPattern.setAttribute("value", "%level")
    layout.appendChild(conversionPattern)
    parameter.appendChild(layout)
    appender.appendChild(parameter)

    parameter = doc.createElement("parameter")
    parameterName = doc.createElement("parameterName")
    parameterName.setAttribute("value", "@logger")
    parameter.appendChild(parameterName)
    dbType = doc.createElement("dbType")
    dbType.setAttribute("value", "String")
    parameter.appendChild(dbType)
    size = doc.createElement("size")
    size.setAttribute("value", "255")
    parameter.appendChild(size)
    layout = doc.createElement("layout")
    layout.setAttribute("type", "log4net.Layout.PatternLayout")
    conversionPattern = doc.createElement("conversionPattern")
    conversionPattern.setAttribute("value", "%logger")
    layout.appendChild(conversionPattern)
    parameter.appendChild(layout)
    appender.appendChild(parameter)

    parameter = doc.createElement("parameter")
    parameterName = doc.createElement("parameterName")
    parameterName.setAttribute("value", "@message")
    parameter.appendChild(parameterName)
    dbType = doc.createElement("dbType")
    dbType.setAttribute("value", "String")
    parameter.appendChild(dbType)
    size = doc.createElement("size")
    size.setAttribute("value", "4000")
    parameter.appendChild(size)
    layout = doc.createElement("layout")
    layout.setAttribute("type", "log4net.Layout.PatternLayout")
    conversionPattern = doc.createElement("conversionPattern")
    conversionPattern.setAttribute("value", "%message")
    layout.appendChild(conversionPattern)
    parameter.appendChild(layout)
    appender.appendChild(parameter)

    parameter = doc.createElement("parameter")
    parameterName = doc.createElement("parameterName")
    parameterName.setAttribute("value", "@exception")
    parameter.appendChild(parameterName)
    dbType = doc.createElement("dbType")
    dbType.setAttribute("value", "String")
    parameter.appendChild(dbType)
    size = doc.createElement("size")
    size.setAttribute("value", "2000")
    parameter.appendChild(size)
    layout = doc.createElement("layout")
    layout.setAttribute("type", "log4net.Layout.ExceptionLayout")
    parameter.appendChild(layout)
    appender.appendChild(parameter)
    return doc


def create_connectionType_element(doc, db_type):
    """
    
    by: 马志 at: ${DATE} ${TIME}
    """
    connectionType = doc.createElement("connectionType")
    if int(db_type) == 0:
        connectionType.setAttribute("value",
                                    "System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.102.0, Culture=neutral")
    if int(db_type) == 1:
        connectionType.setAttribute("value",
                                    "MySql.Data.MySqlClient.MySqlConnection, MySql.Data,Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d")
    if int(db_type) == 2:
        connectionType.setAttribute("value",
                                    "System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    return connectionType


def create_conntection_str_element(doc, db_type, db_name, db_host, db_port, db_username, db_psd):
    """

    by: 马志 at: ${DATE} ${TIME}
    """
    connectionString = doc.createElement("connectionString")
    if int(db_type) == 0:
        connectionStringValue = "Data Source=%s%s;Version=3;" % (get_winservice_path(),db_name)
        connectionString.setAttribute("value", connectionStringValue)
    if int(db_type) == 1:
        connectionStringValue = (
        "Host=%s;UserName=%s;Password=%s;Database=%s;Port=%s;CharSet=utf8;Allow Zero Datetime=true" % (
            db_host, db_username, db_psd, db_name, db_port))
        connectionString.setAttribute("value", connectionStringValue)
    if int(db_type) == 2:
        connectionStringValue = "data source=%s;initial catalog=%s;integrated security=false;persist security info=True;User ID=%s;Password=%s" % (
        db_host, db_name, db_username, db_psd)
        connectionString.setAttribute("value", connectionStringValue)
    return connectionString
